package com.saer.client.menu;

import com.saer.client.SaerEntryPoint;
import com.saer.client.layout.MainLayout;
import com.saer.client.menu.items.AdministrationMenuButton;
import com.saer.client.tab.visita.VisitaTab;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class MenuBar extends ToolStrip {

	private MainLayout mainLayout = null;
	private AdministrationMenuButton administrationMenuButton = new AdministrationMenuButton();
	
	public MenuBar(MainLayout vMainLayout) {
		this.mainLayout = vMainLayout;
		
		this.setWidth("100%");
		this.setAnimateShowTime(200);

		ToolStripButton asistenciaRemota = new ToolStripButton("Asistencia Remota", "icons/16/redcross.png");
		asistenciaRemota.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (!mainLayout.isHasVisitaTab()) {
					mainLayout.setHasVisitaTab(true);
					mainLayout.addTab(new VisitaTab(mainLayout));
				} else {
					SC.warn("Ya se esta ejecutando el modulo de [Asistencia Remota].");
					return;
				}
			}
		});
		
		ToolStripButton enfermeros = new ToolStripButton("Enfermeros", "icons/16/enfermero.png");
		asistenciaRemota.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//mainLayout.addTab(new VisitaTab(mainLayout));
			}
		});
		
		ToolStripButton pacientes = new ToolStripButton("Pacientes", "icons/16/paciente.png");
		asistenciaRemota.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//mainLayout.addTab(new VisitaTab(mainLayout));
			}
		});
		
		ToolStripButton logout = new ToolStripButton("Salir", "icons/16/logout.png");
		logout.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (mainLayout.isHasVisitaTab()) {
					String visitaTabId = mainLayout.getMainTabset().getVisitaTabId();
					((VisitaTab) mainLayout.getMainTabset().getTab(visitaTabId)).stopReload();
				}
				SaerEntryPoint.loadLoginScreen();
			}
		});
		
		Label loginLabel = new Label(createLoginLabel());
		loginLabel.setAlign(Alignment.CENTER);
		loginLabel.setWrap(false);
		
		this.addMember(asistenciaRemota);
		this.addSeparator();
		this.addMember(enfermeros);
		this.addSeparator();
		this.addMember(pacientes);
		this.addSeparator();
		this.addFill();
		this.addSeparator();
		this.addMember(loginLabel);
		this.addSeparator();
		this.addMember(administrationMenuButton);
		this.addSeparator();
		this.addMember(logout);
	}
	
	private String createLoginLabel() {
		StringBuffer sb = new StringBuffer();
		sb.append("<b><font color=\"green\">");
		sb.append(SaerEntryPoint.getLogin().getCentroMedicoName());
		sb.append("</font></b>");
		return sb.toString();
	}
	
}
